/*
 *   Copyright (C) 2006-2007 Jiri Majer. All Rights Reserved.
 *   DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 *   This code is free software; you can redistribute it and/or modify it
 *   under the terms of the GNU General Public License version 2 only, as
 *   published by the Free Software Foundation.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
package org.diabetesdiary.commons.swing.calendar;

import org.joda.time.DateTime;

/**
 *
 * @author Jirka Majer
 */
public class DateTimePanel extends javax.swing.JPanel {

    public static final String DATE_PROPERTY = "datetime.property";
    /** Creates new form DateTimePanel */
    public DateTimePanel() {
        this(null);
    }

    public DateTimePanel(DateTime dateTime) {
        initComponents();
        setDateTime(dateTime == null ? new DateTime() : dateTime);
    }

    public void setDateTime(DateTime dateTime) {
        dateJSpinner1.setDateTime(dateTime);
        timeJSpinner1.setDateTime(dateTime);
    }

    public DateTime getDateTime() {
        if (!timeJSpinner1.isVisible()) {
            return dateJSpinner1.getDateTime().toDateMidnight().toDateTime();
        } else {
            DateTime time = timeJSpinner1.getDateTime();
            return dateJSpinner1.getDateTime().withTime(time.getHourOfDay(), time.getMinuteOfHour(), 0, 0);
        }
    }

    public void setTimeVisible(boolean visible) {
        timeJSpinner1.setVisible(visible);
    }

    public boolean isTimeVisible() {
        return timeJSpinner1.isVisible();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        dateTimePicker1 = new org.diabetesdiary.commons.swing.calendar.DateTimePicker();
        timeJSpinner1 = new org.diabetesdiary.commons.swing.calendar.TimeJSpinner();
        dateJSpinner1 = new org.diabetesdiary.commons.swing.calendar.DateJSpinner();
        jButton1 = new javax.swing.JButton();

        setLayout(new java.awt.GridBagLayout());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        add(timeJSpinner1, gridBagConstraints);

        dateJSpinner1.setMinimumSize(new java.awt.Dimension(100, 20));
        dateJSpinner1.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                dateJSpinner1StateChanged(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        add(dateJSpinner1, gridBagConstraints);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/diabetesdiary/commons/swing/calendar/Calendar_32.png"))); // NOI18N
        jButton1.setBorder(null);
        jButton1.setContentAreaFilled(false);
        jButton1.setPreferredSize(new java.awt.Dimension(32, 32));
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipady = -12;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        add(jButton1, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        DateTime newDate = dateTimePicker1.getDateTimeFromUser(dateJSpinner1.getDateTime(), jButton1);
        if (newDate != null) {
            dateJSpinner1.setDateTime(newDate);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void dateJSpinner1StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_dateJSpinner1StateChanged
        firePropertyChange(DATE_PROPERTY, null, getDateTime());
    }//GEN-LAST:event_dateJSpinner1StateChanged

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.diabetesdiary.commons.swing.calendar.DateJSpinner dateJSpinner1;
    private org.diabetesdiary.commons.swing.calendar.DateTimePicker dateTimePicker1;
    private javax.swing.JButton jButton1;
    private org.diabetesdiary.commons.swing.calendar.TimeJSpinner timeJSpinner1;
    // End of variables declaration//GEN-END:variables
}
